﻿using MIMSCore.Abstract;
using MIMSCore.Logging;
using MIMSDomain.Abstract;
using MIMSDomain.Entities;
using MIMSWeb.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MIMSWeb.Controllers
{
    [Authorize]
    public class MedicalHistoryController : Controller
    {
        ILogger<SynkronLogger> _logger;
        IRepository<Patient> _repository;
        IRepository<DrugHistory> _drugHistoryRepository;

        public MedicalHistoryController(ILogger<SynkronLogger> logger, IRepository<Patient> repository
            , IRepository<DrugHistory> drugHistoryRepository)
        {
            _logger = logger;
            _repository = repository;
            _drugHistoryRepository = drugHistoryRepository;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Search(string PatientCode)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var results = _repository.Search(p => p.Code.Contains(PatientCode));
                    _logger.WriteInformation(2000, "Patient Data Lookup Successfull");

                    return View(results);
                }
            }
            catch (Exception Ex)
            {
                _logger.WriteWarning(3000, "Exception Thrown While Finding Patient Data", Ex);
            }
            
            return View();
        }

        public ActionResult View(int Id)
        {
            try
            {
                var patient = _repository.GetById(Id);

                var medicalHistory = new MedicalHistoryViewModel
                {
                    _patient = patient
                };

                _logger.WriteInformation(2000, "Patient Medical History Retrieved Successfully: ",
                       new Dictionary<string, object> { { "Patient Id", medicalHistory } });

                return View(medicalHistory);
            }
            catch (Exception Ex)
            {
                //Log Exception...
                _logger.WriteCritical(4000, "Error Retrieving Medical History ", Ex);
            }

            return View();
        }

        public ActionResult Add(int Id)
        {
            try
            {
                var result = _repository.Search(p => p.Id == Id).Single();
                _logger.WriteInformation(2000, "Patient Data Lookup Successfull");
                return View(result);
            }
            catch (Exception Ex)
            {
                //log exception...
                _logger.WriteWarning(3000, "Exception Thrown While Finding Patient Data", Ex);
            }

            return View();
        }

        public ActionResult AddDrugHistory(int Id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult AddDrugHistory(DrugHistory Model)
        {
            try
            {
                var patient = _repository.GetById(Model.Id);

                if (patient != null)
                {
                    //Add Drug History to patient
                    var drugHistory = new DrugHistory { 
                            HasMedications = Model.HasMedications,
                             MedicationsPrescribed = Model.MedicationsPrescribed,
                             HasStoppedTakenMedication = Model.HasStoppedTakenMedication,
                             MedicationsPurchased = Model.MedicationsPurchased,
                             TakesMedicationPrescribedForOtherPeople = Model.TakesMedicationPrescribedForOtherPeople,
                             UsesHerbalOrOtherComplementaryMedicines = Model.UsesHerbalOrOtherComplementaryMedicines,
                             UsesInhalers = Model.UsesInhalers,
                             UsesPatches = Model.UsesPatches,
                             UsesSkinCreams = Model.UsesSkinCreams,
                             UsesSuppositories = Model.UsesSuppositories,
                             UsesTabletsToSuck = Model.UsesTabletsToSuck
                    };

                    //_drugHistoryRepository.Save(drugHistory);
                    var collection = new List<DrugHistory>();
                    collection.Add(drugHistory);
                    patient.DrugHistory = collection;

                    _repository.Save(patient);

                    _logger.WriteInformation(2000, "Patient Drug History Updated Successfully..",
                                new Dictionary<string, object> { {"Drug History: ", drugHistory }});
                }
                return View();
            }
            catch (Exception Ex)
            {
                _logger.WriteCritical(4000, "Error Adding Patient ", Ex);
                ModelState.AddModelError("", Ex.Message);
            }
            return View();
        }
    }
}
